package com.twu.biblioteca;

import com.twu.biblioteca.command.Command;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA.
 * User: twer
 * Date: 5/16/13
 * Time: 7:14 PM
 * To change this template use File | Settings | File Templates.
 */
public class ServiceDesk {
    public static String welcomeMessage = "Hello, welcome to Biblioteca Library!\n";
    private String mainMenu;
    private PrintWriter writer;
    private BufferedReader input;
    private StringBuffer messagesBuffer;
    private Library library;
    private Register register;
    private CommandCenter commandCenter;
    private Reader reader;
    private boolean isRunning;

    public ServiceDesk(Library library){
        this.library = library;
        this.register = Register.createRegister();
        this.commandCenter = new CommandCenter();
        this.messagesBuffer = new StringBuffer();
        this.writer = new PrintWriter(System.out);
        this.input = new BufferedReader(new InputStreamReader(System.in));
        this.reader = null;
    }

    public void setWelcomeMessage(String welcomeMessage) {
        this.welcomeMessage = welcomeMessage;
    }

    public String messages() {
        return messagesBuffer.toString();  //To change body of created methods use File | Settings | File Templates.
    }

    public void setMainMenu(String mainMenu) {
        this.mainMenu = mainMenu;
    }

    public String getMainMenu() {
        return mainMenu;
    }

    public void setWriter(PrintWriter writer){
        this.writer = writer;
    }

    public void showMessages(String messages){
        messagesBuffer.delete(0, messagesBuffer.length());
        messagesBuffer.append(messages);
        writer.append(messagesBuffer);
        writer.flush();
    }

    public void welcome(Reader reader) {
        this.reader = reader;
        showMessages(welcomeMessage + menu());
    }

    public void welcomeAndRun(Reader reader){
        this.reader = reader;
        showMessages(welcomeMessage + menu());
        run();
    }

    private void run() {
        isRunning = true;
        while (isRunning){
            String command = readMessage();
            runOption(reader, command);
        }
    }

    public void runOption(Reader reader, String option) {
        Command command = commandCenter.getCommand(option);
        String result = command.service(this, reader);
        showMessages(result);
    }

    private boolean hasOption(String option) {
        if (option.equals("1") || option.equals("2") || option.equals("3") || option.equals("4") || option.equals("0")){
            return true;
        }
        return false;
    }


    public void reserveBookFor(Reader reader, String book) {
        if (library.hasBook(book)){
            reader.keepBook(book);
            String successfulMessage = new String("Thank You! Enjoy the book.");
            showMessages(successfulMessage);
        }
        else{
            String message = "Sorry we don't have that book yet.";
            showMessages(message);
        }
    }

    public boolean isLogin(Reader reader) {
        return register.isLogin(reader);
    }

    public Library library() {
        return library;
    }

    public void authenticate(Reader reader) {
        showMessages("Input your username:");
        String username = readMessage();
        showMessages("Input your password:");
        String password = readMessage();
        reader.setUsername(username);
        reader.setPassword(password);
        register.authenticate(reader);
    }

    private String readMessage() {
        try {
            String message = input.readLine();
            return message;
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }
    public void setInput(BufferedReader input){
        this.input = input;
    }

    public String readerReporter() {
        return register.currentReader().reporter();
    }

    public String menu() {
        return commandCenter.menu();
    }

    public void stop() {
        isRunning = false;
    }

    public String reserve() {

        String book = readMessage();
        return library.reserveBookFor(book, reader);
    }
}
